﻿// ----------------------------------------------------------------------------------
// Sample Code from Fernando Machado Píriz's blog: http://fernandomachadopiriz.com
//
// <copyright company="Fernando Machado Píriz" file="DesktopIconsPart.cs">
// Copyright (c) Fernando Machado Píriz. Distributed under FSF's GPL v2.</copyright>
// ----------------------------------------------------------------------------------
// This code and information are provided "as is" without warranty of any kind,
// either expressed of implied, including but not limited to the implied warranties
// of merchantability and/or fitness for a particular purpose.
// ----------------------------------------------------------------------------------
namespace DesktopIcons
{
    using System.ComponentModel.Composition;
    using System.Windows;
    using System.Windows.Input;
    using GivingAPresentation.Commons;
    
    /// <summary>
    /// An extension part to replace or remove desktop background while the user is delivering a presentation.
    /// </summary>
    [Export(typeof(IContract))]
    [ExportMetadata("Tab", "")]
    public class DesktopIconsPart : IContract
    {
        /// <summary>
        /// The view of this extension part in the Model View View Model pattern.
        /// </summary>
        private DesktopIconsView desktopIconsView;

        /// <summary>
        /// The view model of this extension part in the Model View View Model pattern.
        /// </summary>
        private DesktopIconsViewModel desktopIconsViewModel;

        /// <summary>
        /// The model of this extension part in the Model View View Model pattern.
        /// </summary>        
        private DesktopIconsModel desktopIconsModel;

        /// <summary>
        /// Initializes a new instance of the DesktopIconsPart class.
        /// </summary>
        public DesktopIconsPart()
        {
            this.desktopIconsView = new DesktopIconsView();
            this.desktopIconsViewModel = this.desktopIconsView.Resources["desktopIconsViewModel"] as DesktopIconsViewModel;
            this.desktopIconsModel = this.desktopIconsViewModel.Model;

            this.StartPresentation = new RelayCommand(
                argument => this.desktopIconsViewModel.DoStartPresentation(),
                result => !this.desktopIconsModel.Presenting);

            this.StopPresentation = new RelayCommand(
                argument => this.desktopIconsViewModel.DoStopPresentation(),
                result => this.desktopIconsModel.Presenting);

            this.InitializePart = new RelayCommand(
                settingsManager => this.desktopIconsViewModel.DoInitializePart(),
                result => !this.desktopIconsModel.Presenting);

            this.FinalizePart = new RelayCommand(
                settingsManager => this.desktopIconsViewModel.DoFinalizePart(),
                result => !this.desktopIconsModel.Presenting);
        }

        #region IContract Members

        /// <summary>
        /// Gets the user control of this extension part.
        /// </summary>
        public FrameworkElement Element
        {
            get
            {
                return this.desktopIconsView;
            }
        }

        /// <summary>
        /// Gets the command used to notify component parts to apply changes when the user is starting a presentation. This command has no
        /// arguments.
        /// </summary>
        public ICommand StartPresentation { get; private set; }

        /// <summary>
        /// Gets the command used notify component parts to undo changes when the user ends giving a presentation. This command has no
        /// arguments.
        /// </summary>
        public ICommand StopPresentation { get; private set; }

        /// <summary>
        /// Gets the command used to notify component parts before they are used for the first time. Parts should load previously stored state.
        /// This command receives an <code>ISettingsManager</code> instance as settingsManager.
        /// </summary>
        public ICommand InitializePart { get; private set; }

        /// <summary>
        /// Gets the command used notify component parts when they are not needed anymore. Parts should store currently configured state. This
        /// command receives an <code>ISettingsManager</code> instance as settingsManager.
        /// </summary>
        public ICommand FinalizePart { get; private set; }

        #endregion // IContract Members
    }
}
